package com.hdoit.apollo.web.cargo.apis;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.cargo.model.Unit;
import com.hdoit.apollo.logical.cargo.service.IUnitService;
import com.hdoit.apollo.logical.viewmodel.cargo.UnitSearch;
import com.tennetcn.authority.component.webapi.BaseApi;
import com.tennetcn.authority.component.webapi.ResponseModel;
import com.tennetcn.authority.component.webapi.ResponseStatus;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.data.enums.ModelStatus;

@Controller
@RequestMapping(value = "/api/v1/unit/",method = { RequestMethod.GET,RequestMethod.POST},produces = "application/json;charset=UTF-8")
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(value="/api/v1/unit")
public class UnitApi extends BaseApi{

	@Autowired
	private IUnitService unitService;
	
	private final String getUnitInfoListApi="getUnitInfoList";
	
	private final String saveUnitApi="saveUnit";
	
	private final String deleteUnitApi="deleteUnit";
	
	@RequestMapping(value = deleteUnitApi)
    @ApiOperation(notes = deleteUnitApi, httpMethod = "POST", value = "删除货物信息")
    public @ResponseBody String deleteUnit(String unitId){
    	ResponseModel rm=new ResponseModel();
   
    	if(!unitService.deleteUnit(unitId)) {
    		rm.setStatus(ResponseStatus.dataError);
    	}
    	return rm.toJson();
    }
	
    @RequestMapping(value = getUnitInfoListApi)
    @ApiOperation(notes = getUnitInfoListApi, httpMethod = "GET", value = "计量单位列表")
    public @ResponseBody String getUnitInfoList(){
    	UnitSearch search=new UnitSearch();
    	List<Unit> units = unitService.queryListMP(search,null);
    	
    	ResponseModel rm=new ResponseModel();
    	rm.put("units",units);
    	return rm.toJson();
    }
    
    
    @RequestMapping(value = saveUnitApi)
    @ApiOperation(notes = saveUnitApi, httpMethod = "POST", value = "货品分类信息")
    public @ResponseBody String saveUnit(Unit unit){
    	Unit united=unitService.queryModel(unit.getUnitId());
    	if (united==null) {
    		unit.setCreateDate(DateUtils.getCurrent());
    		unit.setCreateUserId(getLoginId());
    		unit.setCreateUserName(getLoginName());
    		unit.setDeleteMark(YesOrNoInteger.no);
    		unit.setModelStatus(ModelStatus.add);
		}else {
			unit.setModifyDate(DateUtils.getCurrent());
			unit.setModifyUserId(getLoginId());
			unit.setModifyUserName(getLoginName());
			unit.setModelStatus(ModelStatus.update);
			unit.setDeleteMark(united.getDeleteMark());
		}
    	
     	ResponseModel rm=new ResponseModel();
     	if(!unitService.applyChange(unit)){
     		rm.setStatus(ResponseStatus.dataError);
     	}
     	
     	return rm.toJson();
    }
    
}
